<?php
/**
 * Created by PhpStorm.
 * User: wuchao
 * Date: 2020/4/20
 * Time: 11:05
 */
namespace app\api\controller\v1\home;
use app\middleware\ApiAuth;
use think\Request;
use think\facade\Validate;
use think\response\Json;
use think\validate\ValidateRule;
use app\api\model\AddressModel;

class Address
{
    protected $middleware = [ApiAuth::class];

    /**
     * @desc 添加地址
     * @return Json
     */
    public function AddressList(Request $request)
    {
        // 判断参数是否合法
        $input['uid'] = $request->param('uid');//用户ID
        // 参数校验
        $validate = Validate::rule([
            'uid' => ValidateRule::isRequire(),
            ]);
        //逻辑处理
        return AddressModel::AddressList($input);
    }

    /**
     * @desc 添加地址
     * @return Json
     */
    public function AddAddress(Request $request)
    {
        // 判断参数是否合法
        $input['uid'] = $request->param('uid');//用户ID
        $input['name'] = $request->param('name');//收货人姓名
        $input['phone'] = $request->param('phone');//收货人电话
        $input['province'] = $request->param('province');//省
        $input['city'] = $request->param('city');//市
        $input['area'] = $request->param('area');//省
        $input['site'] = $request->param('site');//小区站点
        $input['address'] = $request->param('address');//详细地址
        $input['is_default'] = $request->param('is_default');//是否默认 1：默认
        // 参数校验
        $validate = Validate::rule([
            'uid' => ValidateRule::isRequire(),
            'name' => ValidateRule::isRequire(),
            'phone' => ValidateRule::regex("/^[0-9]{3,11}$/", '请输入准确的手机号码'),
            'province' => ValidateRule::isRequire(),
            'city' => ValidateRule::isRequire(),
            'area' => ValidateRule::isRequire(),
            'site' => ValidateRule::isRequire(),
            'address' => ValidateRule::isRequire(),
            'is_default' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return AddressModel::AddAddress($input);
    }

    /**
     * @desc 修改地址
     * @return Json
     */
    public function EditAddress(Request $request)
    {
        // 判断参数是否合法
        $input['id'] = $request->param('id');//地址ID
        $input['uid'] = $request->param('uid');//用户ID
        $input['name'] = $request->param('name');//收货人姓名
        $input['phone'] = $request->param('phone');//收货人电话
        $input['province'] = $request->param('province');//省
        $input['city'] = $request->param('city');//市
        $input['area'] = $request->param('area');//省
        $input['site'] = $request->param('site');//小区站点
        $input['address'] = $request->param('address');//详细地址
        $input['is_default'] = $request->param('is_default');//是否默认 1：默认
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(),
            'uid' => ValidateRule::isRequire(),
            'name' => ValidateRule::isRequire(),
            'phone' => ValidateRule::regex("/^[0-9]{3,11}$/", '请输入准确的手机号码'),
            'province' => ValidateRule::isRequire(),
            'city' => ValidateRule::isRequire(),
            'area' => ValidateRule::isRequire(),
            'site' => ValidateRule::isRequire(),
            'address' => ValidateRule::isRequire(),
            'is_default' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return AddressModel::EditAddress($input);
    }

    /**
     * @desc 删除地址
     * @return Json
     */
    public function DelAddress(Request $request)
    {
        // 判断参数是否合法
        $input['id'] = $request->param('id');//地址ID
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(),
        ]);
        //逻辑处理
        return AddressModel::DelAddress($input);
    }
}